<%--
  Created by IntelliJ IDEA.
  User: zjsxx
  Date: 2/13/2018
  Time: 8:53 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html style="height: 100%">


<!-- Mirrored from www.zi-han.net/theme/hplus/project_detail.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:19:44 GMT -->
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>H+ 后台主题UI框架 - 项目详情</title>
    <meta name="keywords" content="H+后台主题,后台bootstrap框架,会员中心主题,后台HTML,响应式后台">
    <meta name="description" content="H+是一个完全响应式，基于Bootstrap3最新版本开发的扁平化主题，她采用了主流的左右两栏式布局，使用了Html5+CSS3等现代技术">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">

    <link href="/css/animate.min.css" rel="stylesheet">
    <link href="/css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <!-- jqgrid-->
    <link href="/css/plugins/jqgrid/ui.jqgridffe4.css?0820" rel="stylesheet">
    <!-- Sweet Alert -->
    <link href="/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">

    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap-switch/3.3.4/css/bootstrap3/bootstrap-switch.min.css"
          rel="stylesheet">
    <script type="text/javascript"
            src="http://api.map.baidu.com/api?v=2.0&ak=lhynbQH5Ke8q8H4bGGGeuXbRAXyfblMV"></script>
</head>

<body class="gray-bg">
<div class="row">
    <div class="col-sm-12">
        <div class="wrapper wrapper-content animated fadeInUp">
            <div class="ibox">
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-12">
                            <div class="m-b-md">
                                <div id="execute"></div>
                                <h2>
                                    <div id="thisSensorName"></div>
                                </h2>
                            </div>

                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-5">
                            <dl class="dl-horizontal">

                                <dt>状态：</dt>
                                <div id="state"></div>
                                <br>
                                <dt>种类：</dt>
                                <dd>
                                    <div id="type"></div>
                                </dd>
                            </dl>
                        </div>
                        <div class="col-sm-7" id="cluster_info">
                            <dl class="dl-horizontal">

                                <dt>最后更新：</dt>
                                <dd>
                                    <div id="deviceUpdateTime"></div>
                                </dd>
                                <br>
                                <dt>创建于：</dt>
                                <dd>
                                    <div id="deviceCreateTime"></div>
                                </dd>

                            </dl>
                        </div>
                    </div>

                    <div class="row m-t-sm">
                        <div class="col-sm-12">
                            <div class="panel blank-panel">
                                <div class="panel-heading">
                                    <div class="panel-options">
                                        <ul class="nav nav-tabs">
                                            <li><a href="project_detail.html#tab-1" data-toggle="tab">设备数据</a>
                                            </li>
                                            <li class=""><a href="project_detail.html#tab-2" data-toggle="tab">触发器信息</a>
                                            </li>
                                        </ul>
                                    </div>
                                </div>

                                <div class="panel-body">

                                    <div class="tab-content">
                                        <div class="tab-pane active" id="tab-1">
                                            <input id="my-checkbox" type="checkbox" name="my-checkbox" hidden>
                                            <div class="jqGrid_wrapper">
                                                <table id="table_list_1"></table>
                                                <div id="pager_list_1"></div>
                                            </div>
                                            <div id="allmap" style="width: 100%;height: 300px" hidden></div>
                                            <div id="container" style="height: 550px">

                                            </div>

                                        </div>
                                        <div class="tab-pane" id="tab-2">

                                            <table class="table table-striped">
                                                <thead>
                                                <tr>
                                                    <th>名称</th>
                                                    <th>动作名称</th>
                                                    <th>临界符</th>
                                                    <th>临界数值</th>

                                                </tr>
                                                </thead>
                                                <tbody id="triggerList">

                                                </tbody>
                                            </table>

                                        </div>
                                    </div>

                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div class="modal inmodal" id="editModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content animated bounceInRight">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span
                            aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <i class="fa fa-laptop modal-icon"></i>
                    <h4 class="modal-title">编辑传感器</h4>
                    <small class="font-bold">已经创建的传感器只能修改其名称，要修改状态请删除后新建</small>
                </div>
                <div class="modal-body">
                    <div class="form-group"><label>传感器类型</label> <input id="sensorType"
                                                                        class="form-control" value="ddd" readonly></div>
                    <div class="form-group"><label>传感器名</label> <input id="sensorName" placeholder="请输入您的传感器取一个名字"
                                                                       class="form-control"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button id="updateSensorBtn" type="button" class="btn btn-primary">更新</button>
                    <button id="open" type="button" class="btn btn-primary">启用</button>
                    <button id="close" type="button" class="btn btn-primary">禁用</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal inmodal" id="addTriggerModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content animated bounceInRight">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span
                            aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <i class="fa fa-laptop modal-icon"></i>
                    <h4 class="modal-title">新增触发器</h4>
                    <small class="font-bold">增加一个触发器，在达到条件时触发，请按照文档填写，将不能起效</small>
                </div>
                <div class="modal-body">
                    <div class="form-group"><label>触发器名</label>
                        <input id="triggerName" placeholder="请输入您的触发器的名字" class="form-control">
                    </div>
                    <div class="form-group"><label>符号</label>
                        <select id="triggerCondition" class="form-control">
                        </select>
                    </div>

                    <div class="form-group"><label>数值</label>
                        <input id="triggerNumber" class="form-control">
                        </input>
                    </div>

                    <div class="form-group"><label>动作</label>
                        <select id="actionName" class="form-control">
                        </select>
                    </div>


                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button id="addTriggerBtn" type="button" class="btn btn-primary">新增</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal inmodal" id="updateTriggerModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content animated bounceInRight">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span
                            aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                    </button>
                    <i class="fa fa-laptop modal-icon"></i>
                    <h4 class="modal-title">编辑触发器</h4>
                    <small class="font-bold">编辑触发器，在达到条件时触发，请按照文档填写，将不能起效</small>
                </div>
                <div class="modal-body">
                    <div class="form-group"><label>触发器名</label>
                        <input id="updateTriggerName" placeholder="请输入您的触发器的名字" class="form-control">
                    </div>
                    <div class="form-group"><label>符号</label>
                        <select id="updateTriggerCondition" class="form-control">
                        </select>
                    </div>

                    <div class="form-group"><label>数值</label>
                        <input id="updateTriggerNumber" class="form-control">
                        </input>
                    </div>

                    <div class="form-group"><label>动作</label>
                        <select id="updateTctionName" class="form-control">
                        </select>
                    </div>
                    <input type="text"  id="hiddenTriggerId" hidden>

                </div>
                <div class="modal-footer">
                    <button id="closeUpdteTrigger" type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button id="updateTriggerBtn" type="button" class="btn btn-primary">更新</button>
                </div>
            </div>
        </div>
    </div>

</div>


</div>
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.6"></script>
<script src="/js/content.min.js?v=1.0.0"></script>
<script src="/js/plugins/sweetalert/sweetalert.min.js"></script>
<script src="/js/jquery.json.js"></script>

<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>


<script type="text/javascript" src=""></script>
<script type="text/javascript" src="/js/remote/echarts.min.js"></script>
<script type="text/javascript" src="/js/remote/echarts-gl.min.js"></script>
<script type="text/javascript" src="/js/remote/dataTool.min.js"></script>
<script type="text/javascript" src="/js/remote/china.js"></script>
<script type="text/javascript" src="/js/remote/world.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=ZUONbpqGBsYGXNIYHicvbAbM"></script>
<script type="text/javascript" src="/js/remote/bmap.min.js"></script>
<script type="text/javascript" src="/js/remote/simplex.js"></script>
<script src="/js/remote/bootstrap-switch.min.js"></script>
<script src="/js/plugins/peity/jquery.peity.min.js"></script>
<script src="/js/plugins/jqgrid/i18n/grid.locale-cnffe4.js?0820"></script>
<script src="/js/plugins/jqgrid/jquery.jqGrid.minffe4.js?0820"></script>
<script src="/js/content.min.js?v=1.0.0"></script>


<script type="text/javascript">
    function  updateList(message) {
        var data = message.data;
        for (var i = 0; i < data.length; i++) {
            var item = data[i];
            var cond = item.triggerCondition;
            if (cond == '1') {
                cond = '=';
            } else if (cond == '2') {
                cond = '>=';
            } else if (cond == '3') {
                cond = '>';
            } else if (cond == '4') {
                cond = '<';
            } else if (cond == '5') {
                cond = '<='
            }
            var number = item.triggerNumber;
            console.log(item.triggerNumber);
            if (sensorType == 1) {
                if (number == 1) number = '闭合';
                else number = '松开';
            }
            var id = item.triggerId;
            var line = "<tr><td>" + item.triggerName
                + "</td><td>" + item.actionName
                + "</td><td>" + cond
                + "</td><td>" + number
                + "</td><td>" + "<button onclick='editTriggerModal(" + id + ")'  type=\"button\" data-toggle=\"modal\" data-target=\"#updateTriggerModal\" class=\"btn btn-warning btn-xs pull-right\">编辑触发器</button>"
                + "<button  onclick='delTrigger(" + id + ")' type=\"button\" class=\"btn btn-danger btn-xs pull-right\" >删除</button>" +
                "</td></tr>";
            $('#triggerList').append(line);
        }
    }
    //获取参数的函数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return decodeURI(r[2]);
        return null; //返回参数值
    }
    var sensorId =  getUrlParam('sensorId');
    //删除触发器
    function delTrigger(number) {
        var key = null;
        var v = document.cookie.split(";");
        for (var i = 0; i < v.length; i++) {
            var item = v[i].split("=");
            if (item[0] == 'easylink_user_token') {
                key = item[1];
                break;
            }
        }
        $.ajax({
            type: 'GET',
            url: '/trigger/deleteTrigger/' + number,
            headers: {
                'easylink_user_token': key
            },
            success: function (message) {
                $.ajax({
                    type: 'GET',
                    url: '/trigger/getSensorAllTriggers/' + sensorId,
                    headers: {
                        'easylink_user_token': key
                    },
                    success: function (message) {

                        $('#triggerList').empty();
                        updateList(message);

                    },

                    error: function (message) {
                        console.log(message);
                    }

                });
            },
            error: function (message) {
                console.log(message);
            }

        });
    }
    function editTriggerModal(number) {
        var key = null;
        var v = document.cookie.split(";");
        for (var i = 0; i < v.length; i++) {
            var item = v[i].split("=");
            if (item[0] == 'easylink_user_token') {
                key = item[1];
                break;
            }
        }
        $.ajax({
            type: 'GET',
            url: '/trigger/getTriggerInfo/' + number,
            headers: {
                'easylink_user_token': key
            },
            success: function (message) {
                var data = message.data;
                console.log(data);
                $('#updateTriggerName').val(data.triggerName);
                $('#updateTriggerNumber').val(data.triggerNumber);
                var value = "";
                if(value == 1) value = '=';
                if(value == 2) value = '>=';
                if(value == 3) value = '>';
                if(value == 4) value = '<';
                if(value == 5) value = '<=';
                $('#hiddenTriggerId').val(data.triggerId);
                $("#updateTriggerCondition").find("option[value = '"+value+"']").attr("selected","selected");
                $("#updateTctionName").find("option[value = '"+data.actionId+"']").attr("selected","selected");
            },
            error: function (message) {
                console.log(message);
            }

        });
    }

    $(document).ready(function () {
        var state;

        //获取参数的函数
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return decodeURI(r[2]);
            return null; //返回参数值
        }

        //获取userKeY
        var key = null;
        var v = document.cookie.split(";");
        for (var i = 0; i < v.length; i++) {
            var item = v[i].split("=");
            if (item[0] == 'easylink_user_token') {
                key = item[1];
                break;
            }
        }


        //传感器编号
        var sensorId = getUrlParam("sensorId");
        var sensorType = getUrlParam("sensorType");
        var sensorTypeText = '未知类型';
        if (sensorType == 1) sensorTypeText = '开关';
        if (sensorType == 2) sensorTypeText = '数值';
        if (sensorType == 3) sensorTypeText = 'GPS';


        $('#sensorType').val(sensorTypeText);
        /**删除操作**/
        $('#execute').append(" <a href=\"#\" id='delDevice' class=\"btn btn-danger btn-xs pull-right\">删除传感器</a>\n");
        $('#execute').append("<button type=\"button\" data-toggle=\"modal\" data-target=\"#addTriggerModal\" class=\"btn btn-warning btn-xs pull-right\">新增触发器</button>");
        ;
        $('#execute').append("<button type=\"button\" data-toggle=\"modal\" data-target=\"#editModal\" class=\"btn btn-default btn-xs pull-right\">编辑传感器</button>");
        ;


        var dom = document.getElementById("container");
        var myChart = echarts.init(dom);
        var app = {};
        option = null;

        function splitData(rawData) {

            var values = [];
            var categoryData = [];
            for (var i = 0; i < rawData.length; i++) {
                var item = rawData[i];
                values.push(item.sensorData);
                categoryData.push(item.dataTime);
            }
            return {
                categoryData: categoryData,
                values: values,
                volumns: values
            };
        }

        function getLineInfo(sensorId) {
            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/data/searchSensorDate',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "sensorId": sensorId
                }),
                success: function (message) {
                    rawData = message.data;
                    var data = splitData(rawData);
                    myChart.setOption(option = {
                        backgroundColor: '#eee',
                        animation: true,

                        legend: {
                            bottom: 10,
                            left: 'center',
                            data: ['数值']
                        },
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'cross'
                            },
                            backgroundColor: 'rgba(245, 245, 245, 0.8)',
                            borderWidth: 1,
                            borderColor: '#ccc',
                            padding: 10,
                            textStyle: {
                                color: '#000'
                            },
                            formatter: function (params) {
                                var returnStr = params[0].axisValue + "<br>" + "数值:" + params[0].data;
                                return returnStr;
                            },
                            position: function (pos, params, el, elRect, size) {
                                var obj = {top: 10};
                                obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30;
                                return obj;
                            },
                            extraCssText: 'width: 170px'
                        },
                        axisPointer: {
                            link: {xAxisIndex: 'all'},
                            label: {
                                backgroundColor: '#777'
                            }
                        },
                        toolbox: {
                            feature: {
                                dataZoom: {
                                    yAxisIndex: false
                                },
                                brush: {
                                    type: ['lineX', 'clear']
                                }
                            }
                        },
                        brush: {
                            xAxisIndex: 'all',
                            brushLink: 'all',
                            outOfBrush: {
                                colorAlpha: 0.1
                            }
                        },
                        grid: [
                            {
                                left: '10%',
                                right: '8%',
                                height: '50%'
                            },
                            {
                                left: '10%',
                                right: '8%',
                                bottom: '20%',
                                height: '15%'
                            }
                        ],
                        xAxis: [
                            {
                                type: 'category',
                                data: data.categoryData,
                                scale: true,
                                boundaryGap: false,
                                axisLine: {onZero: false},
                                splitLine: {show: false},
                                splitNumber: 20,
                                min: 'dataMin',
                                max: 'dataMax',
                                axisPointer: {
                                    z: 100
                                }
                            },
                            {
                                type: 'category',
                                gridIndex: 1,
                                data: data.categoryData,
                                scale: true,
                                boundaryGap: false,
                                axisLine: {onZero: false},
                                axisTick: {show: false},
                                splitLine: {show: false},
                                axisLabel: {show: false},
                                splitNumber: 20,
                                min: 'dataMin',
                                max: 'dataMax',
                                axisPointer: {
                                    label: {
                                        formatter: function (params) {
                                            return params.value;
                                        }
                                    }
                                }
                            }
                        ],
                        yAxis: [
                            {
                                scale: true,
                                splitArea: {
                                    show: false
                                }
                            },
                            {
                                scale: true,
                                gridIndex: 1,
                                splitNumber: 2,
                                axisLabel: {show: false},
                                axisLine: {show: false},
                                axisTick: {show: false},
                                splitLine: {show: false}
                            }
                        ],
                        dataZoom: [

                            {
                                show: true,
                                xAxisIndex: [0, 1],
                                type: 'slider',
                                top: '85%',
                                start: 0,
                                end: 100
                            }
                        ],
                        series: [


                            {
                                name: '柱体',
                                type: 'bar',
                                xAxisIndex: 1,
                                yAxisIndex: 1,
                                data: data.volumns
                            },
                            {
                                name: '曲线',
                                type: 'line',
                                data: data.values,
                                smooth: true,
                                lineStyle: {
                                    normal: {opacity: 0.5}
                                }
                            }
                        ]
                    }, true);
                },
                error: function (message) {
                    console.log(message);
                }
            });
            if (option && typeof option === "object") {
                myChart.setOption(option, true);
            }
        }

        function getSwitchInfo() {
            $('#container').hide();
            $("[name='my-checkbox']").bootstrapSwitch({
                onText: "打开",
                offText: "关闭",
                onColor: "success",
                offColor: "warning",
                size: "large",
                //当开关状态改变的时候触发
                onSwitchChange: function (event, state) {
                    var data = (state == true) ? 1 : 0;
                    //发送 改变开关的请求
                    $.ajax({
                        type: 'POST',
                        headers: {
                            'easylink_user_token': key
                        },
                        url: '/data/insertData',
                        dataType: 'json',
                        contentType: "application/json; charset=utf-8",
                        data: $.toJSON({
                            "sensorId": sensorId,
                            "sensorData": data
                        }),
                        success: function (message) {
                            //重新获取数据
                            $.ajax({
                                type: 'POST',
                                headers: {
                                    'easylink_user_token': key
                                },
                                url: '/data/searchSensorDate',
                                dataType: 'json',
                                contentType: "application/json; charset=utf-8",
                                data: $.toJSON({
                                    "sensorId": sensorId
                                }),
                                success: function (message) {
                                    $("#table_list_1").jqGrid('clearGridData');
                                    $("#table_list_1").jqGrid('setGridParam', {  // 重新加载数据
                                        data: message.data,   //  newdata 是符合格式要求的需要重新加载的数据
                                    }).trigger("reloadGrid");
                                }
                            });
                        },
                        error: function (message) {
                            console.log(message);
                        }
                    });


                }
            });
            // $("[name='my-checkbox']").show();
            //获取开关的状态
            $.ajax({
                type: 'GET',
                url: '/sensor/getSwitchState/' + sensorId,
                headers: {
                    'easylink_user_token': key
                },
                success: function (message) {
                    if (message.data == 1) {
                        $("[name='my-checkbox']").bootstrapSwitch('toggleState', true);
                    }
                },
                error: function (message) {
                    console.log(message);
                }

            });
            /**
             * 获取开关的所有历史状态
             * **/

            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/data/searchSensorDate',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "sensorId": sensorId
                }),
                success: function (message) {
                    console.log(message);
                    switchData = message.data;
                    //表格数据
                    $.jgrid.defaults.styleUI = "Bootstrap";

                    $("#table_list_1").jqGrid({
                        data: switchData,
                        datatype: "local",
                        height: 250,
                        autowidth: true,
                        shrinkToFit: true,
                        rowNum: 10,
                        rowList: [10, 20, 30],
                        colNames: ["日期", "传感器编号", "事件", "传感器名"],
                        colModel: [
                            {
                                name: "dataTime",
                                index: "dataTime",
                                width: 90,
                                sorttype: "date"
                            }, {name: "sensorId", index: "sensorId", width: 100}, {
                                name: "sensorData",
                                index: "sensorData",
                                width: 80,
                                align: "right",
                                sorttype: "int",
                                formatter: function checkboxFormatter(cellvalue, options, rowObject) {
                                    if (cellvalue == 1) {
                                        return '开启'
                                    } else {
                                        return '关闭';
                                    }
                                }
                            }, {name: "sensorName", index: "sensorName", width: 80, align: "right", sorttype: "float"}],
                        pager: "#pager_list_1",
                        viewrecords: true,
                        caption: "历史数据",
                        hidegrid: false
                    });
                    $(window).bind("resize", function () {
                        var width = $(".jqGrid_wrapper").width();
                        $("#table_list_1").setGridWidth(width);
                        $("#table_list_2").setGridWidth(width)
                    })
                },
                error: function (message) {
                    console.log(message);
                }
            });


        }

        function getGpsInfo() {
            $('#container').hide();
            //发送get请求获取GPS传感器所有的数据
            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/data/searchSensorDate',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "sensorId": sensorId,
                }),
                success: function (message) {
                    var  data  = message.data;
                    var i = 0;
                    var pointList=[];
                    for(;i<data.length;i++){
                        var item = data[i];
                        var list = item.sensorData.split('#');
                        pointList[i] = [list[0],list[1],item.dataTime+" "+"<br>经度:"+list[0]+"<br>纬度:"+list[1]];
                    }
                    console.log(message);
                    console.log(pointList);
                    // 百度地图定点
                    // 百度地图API功能
                    $('#allmap').show();
                    map = new BMap.Map("allmap");
                    map.addControl(new BMap.NavigationControl());
                    map.centerAndZoom(new BMap.Point(pointList[0][0],pointList[0][1]), 15);
                    var data_info = pointList;
                    var opts = {
                        width : 250,     // 信息窗口宽度
                        height: 80,     // 信息窗口高度
                        title : "具体信息" , // 信息窗口标题
                        enableMessage:true//设置允许信息窗发送短息
                    };
                    for(var i=0;i<data_info.length;i++){
                        var marker = new BMap.Marker(new BMap.Point(data_info[i][0],data_info[i][1]));  // 创建标注
                        var content = data_info[i][2];
                        map.addOverlay(marker);               // 将标注添加到地图中
                        addClickHandler(content,marker);
                    }
                    //绘图
                    var p = 0;
                    for(;p<pointList.length-1;p++){
                        var polyline = new BMap.Polyline([
                        new BMap.Point(pointList[p][0],pointList[p][1]),//起始点的经纬度
                        new BMap.Point(pointList[p+1][0],pointList[p+1][1])//终止点的经纬度
                    ], {strokeColor:"red",//设置颜色
                        strokeWeight:3, //宽度
                        strokeOpacity:0.5});//透明度
                    map.addOverlay(polyline);
                    }

                    function addClickHandler(content,marker){
                        marker.addEventListener("click",function(e){
                            openInfo(content,e)}
                        );
                    }
                    function openInfo(content,e){
                        var p = e.target;
                        var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
                        var infoWindow = new BMap.InfoWindow(content,opts);  // 创建信息窗口对象
                        map.openInfoWindow(infoWindow,point); //开启信息窗口
                    }

                },
                error: function (message) {
                    console.log(message);
                }
            });





        }


        var type = getUrlParam("sensorType");
        /**
         * 开关=1
         * 数值=2
         * GPS=3
         */
        if (type == 1) {
            getSwitchInfo();
        }
        if (type == 2) {
            getLineInfo(sensorId);
        }
        if (type == 3) {
            getGpsInfo();
        }


        /*启用*/
        $('#open').click(function () {
            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/sensor/updateSensor',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "sensorId": sensorId,
                    "sensorState": 1
                }),
                success: function (message) {
                    $(location).attr('href', window.location.pathname + window.location.search);
                },
                error: function (message) {
                    console.log(message);
                }
            });
        });
        /*禁用*/
        $('#close').click(function () {
            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/sensor/updateSensor',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "sensorId": sensorId,
                    "sensorState": 0
                }),
                success: function (message) {
                    $(location).attr('href', window.location.pathname + window.location.search);
                },
                error: function (message) {
                    console.log(message);
                }
            });
        });
        /**
         * 点击更新按钮，更新传感器信息
         */
        $('#updateSensorBtn').click(function () {
            //更新
            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/sensor/updateSensor',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "sensorId": sensorId,
                    "sensorName": $('#sensorName').val()
                }),
                success: function (message) {
                    $(location).attr('href', window.location.pathname + window.location.search);
                },
                error: function (message) {
                    console.log(message);
                }
            });
        });

        //发送ajax请求
        $.ajax({
            type: 'GET',
            url: '/sensor/getSensorInfo/' + sensorId,
            headers: {
                'easylink_user_token': key
            },
            success: function (message) {
                $('#thisSensorName').append(message.data.sensorName);

                var data = message.data;
                //状态
                state = data.sensorState;
                //$('#state').append("<input id='state-checkbox' type='checkbox'>");
                if (data.sensorState == 1) {
                    $('#state').append("<dd><span class=\"label label-primary\">正常</span></dd>");
                    $('#open').hide();
                    // $("[name='my-checkbox']").show();
                    // $('#execute').append("<button id=\"updateSensorBtn\" type=\"button\" class=\"btn btn-primary\">保存</button>");
                } else {
                    $('#state').append("<dd><span class=\"label label-danger\">停用</span></dd>");
                    $('#close').hide();
                    // $("[name='my-checkbox']").hide();
                }
                //最后更新于
                $('#deviceUpdateTime').append(data.sensorUpdatetime);
                //创建时间
                $('#deviceCreateTime').append(data.sensorCreatetime);
                //种类
                var t = '未知种类传感器';
                if (data.sensorType == 1) {
                    t = "开关";
                } else if (data.sensorType == 2) {
                    t = "数值型传感器";
                } else if (data.sensorType == 3) {
                    t = "GPS";
                }
                $('#type').append(t);
            },
            error: function (message) {
                console.log(message);
            }
        });
        //符号下拉框
        if (sensorType == 1) {
            $('#triggerCondition').append("<option value =\"=\">=</option>");
            $('#updateTriggerCondition').append("<option value =\"=\">=</option>");

        }
        if (sensorType == 2) {
            $('#updateTriggerCondition').append("<option value =\">\">></option>");
            $('#updateTriggerCondition').append("<option value =\">=\">>=</option>");
            $('#updateTriggerCondition').append("<option value =\"=\">=</option>");
            $('#updateTriggerCondition').append("<option value =\"<\"><</option>");
            $('#updateTriggerCondition').append("<option value =\"<=\"><=</option>");

            $('#triggerCondition').append("<option value =\">\">></option>");
            $('#triggerCondition').append("<option value =\">=\">>=</option>");
            $('#triggerCondition').append("<option value =\"=\">=</option>");
            $('#triggerCondition').append("<option value =\"<\"><</option>");
            $('#triggerCondition').append("<option value =\"<=\"><=</option>");
        }
        if (sensorType == 3) {
            $('#triggerCondition').append("<option value =\"<=\">圈内</option>");
            $('#triggerCondition').append("<option value =\">\">圈外</option>");

            $('#updateTriggerCondition').append("<option value =\"<=\">圈内</option>");
            $('#updateTriggerCondition').append("<option value =\">\">圈外</option>");
        }
        //动作下拉框
        $.ajax({
            type: 'GET',
            url: '/action/selectUserAction',
            headers: {
                'easylink_user_token': key
            },
            success: function (message) {
                var data = message.data;
                for (var i = 0; i < data.length; i++) {
                    var item = data[i];
                    $('#actionName').append("<option value =" + item.actionId + ">" + item.actionName + "</option>");
                    $('#updateTctionName').append("<option value =" + item.actionId + ">" + item.actionName + "</option>");

                }
            },
            error: function (message) {
                console.log(message);
            }
        });

        /**
         * 获取一个传感器下所有的触发器
         * **/
        $.ajax({
            type: 'GET',
            url: '/trigger/getSensorAllTriggers/' + sensorId,
            headers: {
                'easylink_user_token': key
            },
            success: function (message) {


               updateList(message);
            },

            error: function (message) {
                console.log(message);
            }

        });



        $('#addTriggerBtn').click(function () {
            var triggerName = $('#triggerName').val();
            var triggerCondition = $('#triggerCondition').val();
            var triggerNumber = $('#triggerNumber').val();
            var actionId = $('#actionName').val();
            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/trigger/insertTrigger',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "sensorId": sensorId,
                    "triggerName": triggerName,
                    "triggerCondition": triggerCondition,
                    "triggerNumber": triggerNumber,
                    "actionId": actionId
                }),
                success: function (message) {
                    $('#addTriggerModal').modal('hide');
                    $.ajax({
                        type: 'GET',
                        url: '/trigger/getSensorAllTriggers/' + sensorId,
                        headers: {
                            'easylink_user_token': key
                        },
                        success: function (message) {

                            $('#triggerList').empty();
                            updateList(message);

                        },

                        error: function (message) {
                            console.log(message);
                        }

                    });
                }
            });
        });


        $('#updateTriggerBtn').click(function () {
            var triggerName = $('#updateTriggerName').val();
            var triggerCondition = $('#updateTriggerCondition').val();
            var triggerNumber = $('#updateTriggerNumber').val();
            var actionId = $('#updateTctionName').val();
            $.ajax({
                type: 'POST',
                headers: {
                    'easylink_user_token': key
                },
                url: '/trigger/updateTrigger',
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                data: $.toJSON({
                    "triggerId":$('#hiddenTriggerId').val(),
                    "sensorId": sensorId,
                    "triggerName": triggerName,
                    "triggerCondition": triggerCondition,
                    "triggerNumber": triggerNumber,
                    "actionId": actionId
                }),
                success: function (message) {
                    $('#updateTriggerModal').modal('hide');
                    $.ajax({
                        type: 'GET',
                        url: '/trigger/getSensorAllTriggers/' + sensorId,
                        headers: {
                            'easylink_user_token': key
                        },
                        success: function (message) {

                            $('#triggerList').empty();
                            updateList(message);

                        },

                        error: function (message) {
                            console.log(message);
                        }

                    });
                }
            });
        });

    });


</script>

</body>

</html>
